﻿$(function () { 
    $("#ds-category ul li").live("click", function () {
        var $li = $(this).addClass("ds-category-select");
        $li.siblings().removeClass("ds-category-select");
        $("#CategoryID").val($li.attr("data-id"));
        $("#Code").val($li.attr("data-code"));
        $("#FullCategoryName").val($li.attr("data-path").replace(new RegExp("/", "g"), " > "));
        var after_ul = $li.parent("ul").nextAll("ul");
        if (after_ul != undefined && after_ul.length > 0) {
            for (var i = 0; i < after_ul.length; i++) {
                document.getElementById("ds-category").removeChild(after_ul[i])
            }
        } 
        $.post("/Home/GetPartialCategory", { pid: $li.attr("data-id") }, function (data) {
            var $ul = $("<ul></ul>");
            if (data.length > 0) {
                $.each(data, function () {
                    var $cli = $("<li data-id='" + this.CategoryID
                        + "' data-code='" + this.CategoryCode
                        + "' data-level='" + this.CategoryLevel
                        + "' data-pid='" + this.PCategoryID
                        + "' data-path='" + this.FullCategoryName
                        + "'></li>");
                    var $a = $("<a href='javascript:void(0)'>" + this.CategoryNameDesc
                        + "</a><em class='icon-caret-right'></em>");
                    $cli.append($a);
                    $ul.append($cli);
                });
            }
            else {
                $ul.append("<div style='width:100%;height:100%;margin:0px;position:relative;'><button type='button' id='category_ok' style='position:absolute;top:50%;left:50%;margin:-5% 0 0 -15%;' class='btn'>确 定</button></div>");
            }
            $("#ds-category").append($ul);
        });
    });
    $("#category_ok").live("click", function () {
        var dataCode = $("#Code").val();
        var regExp = /^[A-Za-z]{2}$/.test(dataCode);
        if (regExp) {
            $.get("/Home/GetMaxCode", { id: $("#Code").val() }, function (data) {
                $("#sku_remark").html(data);
            });
        }
    });
});
